<?php
namespace  App\Http\Controllers\Radius;
use  App\Http\Controllers\Controller;

use App\Model\Rad_line;
use App\Order;

use App\Teamqq;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;

use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;


use App\Radgroupreply;
use App\Rad_users;
use App\Rad_order;
use App\Line;
use DB;

use App\Agent;
use App\External_line;
use App\Bind_wechat_public_number;

use App\Http\Controllers\Radius\ModuleController;


class  WechatPublicNumberController extends Controller
{

    public function bindUserWechat(Request $request)
    {

        $params = [];
        $mobile = $request->input('mobile');
        $app_id = $request->input('fromusername');
        $timestamp = $request->input('timestamp');

        $users = Rad_users::where('mobile',$mobile)->first();


        if($users == NULL){
            self::ShowError(ErrorDefine::MOBILE_NOT_EXIST);
            return;
        }

        $bind_mobile = Bind_wechat_public_number::where('mobile',$mobile)->first();
        $bind_app_id = Bind_wechat_public_number::where('app_id',$app_id)->first();

        if($bind_mobile != NULL || $bind_app_id != NULL)
        {
            self::ShowError(ErrorDefine::MOBILE_BIND_EXISTS);
            return;
        }

        try{
            $ret = Bind_wechat_public_number::insertGetId(['mobile'=>$mobile,'app_id'=>$app_id]);

            if($ret){

                $expired_at = '';

                if(strtotime($users->expired_at) > time())
                {
                    $expired_at = strtotime($users->expired_at) + 60*30;
                }else{
                    $expired_at = time() + 60*30;
                }

                $radcheck_time = date("d M Y H:i:s ",$expired_at);
                $user_time = date('Y-m-d H:i:s',$expired_at);

                $users->expired_at = $user_time;

                Radcheck::where('username',$users->username)->where('attribute','=','Expiration')->update(['value'=>$radcheck_time]);

                $users->save();

            }

            return self::ShowMessage();


        }catch (\Exception $e){
            self::ShowError(ErrorDefine::BIND_FALSE);
            return;
        }


    }


}
